package com.examiner.service.impl;

import com.examiner.bean.CommonPerson;
import com.examiner.bean.Department;
import com.examiner.dao.CommonPersonDao;
import com.examiner.dao.DepartmentDao;
import com.examiner.service.DepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentDao departmentDao;
    @Resource
    private CommonPersonDao commonPersonDao;

    @Override
    public Department findByUserName(String userName) {
        return departmentDao.findByUserName(userName);
    }

    /**
     * 新增department
     * @param department
     * @return
     */
    @Override
    public Department addDepartment(Department department) {
        return departmentDao.save(department);
    }

    /**
     * 根据 上级 获取 部门列表
     * @param superior
     * @return
     */
    @Override
    public List<Department> getDepartmentList(String superior) {
        return departmentDao.findBySuperior(superior);
    }

    /**
     * 判断是否存在
     * @param id
     * @return
     */
    @Override
    public Boolean existsById(Integer id) {
        return departmentDao.existsById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Boolean removeDepartment(Integer id) {
        Department department = departmentDao.getOne(id);
        List<CommonPerson> commonPersonList = commonPersonDao.findBySuperior(department.getUserName());
        for (CommonPerson commonPerson : commonPersonList) {
            commonPersonDao.delete(commonPerson);
        }
        departmentDao.deleteById(id);
        return departmentDao.existsById(id);
    }

    /**
     * 修改信息
     * @param department
     * @return
     */
    @Override
    public Department changeDepartmentInfo(Department department) {
        return departmentDao.save(department);
    }

    /**
     * 获取所有 下级部门
     * @param superior
     * @return
     */
    @Override
    public List<Department> getAllDepartmentList(String superior) {
        return null;
    }

    /**
     * 改变部门考评员
     * @param department
     * @throws Exception
     */
    @Override
    public void changeExaminer(Department department) throws Exception {
        departmentDao.changeExaminer(department.getId(),department.getEid(),department.getExaminer());
    }

    @Override
    public Department findById(Integer id) throws Exception {
        return departmentDao.getOne(id);
    }
}
